#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
using namespace std;
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib/BaseLib_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_XRpc/XRPC_Error.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_APIHelp/APIHelp_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_NetHelp/NetHelp_APIHelp/APIHelp_Error.h"

//g++ -std=gnu++11 -Wall -g Test_XRPCClient.cpp -o Test_XRPCClient -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_HelpComponents -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_NetHelp -lNetEngine_BaseLib -lNetEngine_Algorithm -lNetEngine_Core -lNetEngine_ManagePool -lNetEngine_OPenSsl -lNetClient_Socket -lHelpComponents_Packet -lRfcComponents_HttpServer -lNetHelp_XRpc -lNetHelp_APIHelp -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_HelpComponents:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_NetHelp,--disable-new-dtags

void __stdcall XRPCClient_Callback(XNETHANDLE xhToken, LPCTSTR lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE nRtnType, NETHELP_XRPCPARAMETE* pSt_XRPCParamete, LPVOID lParam)
{

}
XNETHANDLE xhToken;
int XEngine_AddMethod(int a, int b)
{
    list<NETHELP_XRPCPARAMETE> stl_ListParamete;

    NETHELP_XRPCPARAMETE st_XPRCReturn;
    memset(&st_XPRCReturn, '\0', sizeof(NETHELP_XRPCPARAMETE));

    st_XPRCReturn.lParameteValue = malloc(1000);
    memset(st_XPRCReturn.lParameteValue, '\0', 1000);

    NetHelp_XRPCHelp_Malloc(&stl_ListParamete, 2);

    list<NETHELP_XRPCPARAMETE>::iterator stl_ListIterator = stl_ListParamete.begin();
    stl_ListIterator->enXRPC_ParameteType = ENUM_NETHELP_XRPC_PARAMETE_TYPE_INT;
    stl_ListIterator->nParameteLen = sizeof(int);
    memcpy(stl_ListIterator->lParameteValue, &a, sizeof(int));

    stl_ListIterator++;
    stl_ListIterator->enXRPC_ParameteType = ENUM_NETHELP_XRPC_PARAMETE_TYPE_INT;
    stl_ListIterator->nParameteLen = sizeof(int);
    memcpy(stl_ListIterator->lParameteValue, &b, sizeof(int));

    LPCTSTR lpszFuncName = _T("XEngine_AddMethod");
    if (!NetHelp_XRPCClient_ReqFuncCall(xhToken, lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE_INT, &stl_ListParamete, &st_XPRCReturn))
    {
        printf("NetHelp_XRPCClient_ReqFuncCall1:%lX\n", XRpc_GetLastError());
        return 0;
    }
    NetHelp_XRPCHelp_Free(&stl_ListParamete);
    stl_ListParamete.clear();

    int nRet = 0;
    memcpy(&nRet, st_XPRCReturn.lParameteValue, sizeof(int));
    return nRet;
}
void XEngine_Print(LPCTSTR lpszString)
{
    list<NETHELP_XRPCPARAMETE> stl_ListParamete;

    NETHELP_XRPCPARAMETE st_XPRCReturn;
    memset(&st_XPRCReturn, '\0', sizeof(NETHELP_XRPCPARAMETE));

    st_XPRCReturn.lParameteValue = malloc(1000);
    memset(st_XPRCReturn.lParameteValue, '\0', 1000);

    NetHelp_XRPCHelp_Malloc(&stl_ListParamete, 1);

    list<NETHELP_XRPCPARAMETE>::iterator stl_ListIterator = stl_ListParamete.begin();
    stl_ListIterator->enXRPC_ParameteType = ENUM_NETHELP_XRPC_PARAMETE_TYPE_STRING;
    stl_ListIterator->nParameteLen = _tcslen(lpszString);
    memcpy(stl_ListIterator->lParameteValue, lpszString, stl_ListIterator->nParameteLen);

    LPCTSTR lpszFuncName = _T("XEngine_Print");
    if (!NetHelp_XRPCClient_ReqFuncCall(xhToken, lpszFuncName, ENUM_NETHELP_XRPC_PARAMETE_TYPE_VOID, &stl_ListParamete))
    {
        printf("NetHelp_XRPCClient_ReqFuncCall2:%lX\n", XRpc_GetLastError());
        return;
    }
    NetHelp_XRPCHelp_Free(&stl_ListParamete);
    stl_ListParamete.clear();
}
int XEngine_HttpAdd(int a, int b)
{
    int nCode = 0;
    int nLen = 1024;
    TCHAR tszMsgBuffer[1024];
    TCHAR tszFileBuffer[1024];
    LPCTSTR lpszHttpAddr = _T("http://127.0.0.1:50002/XRPCApi/FuncCall");
    LPCTSTR lpszJsonFile = _T("./1.txt");

    memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
    memset(tszFileBuffer, '\0', sizeof(tszFileBuffer));

    FILE* pSt_File = _tfopen(lpszJsonFile, "rb");
    int nRet = fread(tszFileBuffer, 1, sizeof(tszFileBuffer), pSt_File);
    fclose(pSt_File);

    APIHelp_HttpRequest_Post(lpszHttpAddr, &nCode, tszFileBuffer, tszMsgBuffer, &nLen);
    printf("%s\n", tszMsgBuffer);
    return nRet;
}
int main()
{
    LPCTSTR lpszAddr = _T("127.0.0.1");
    if (!NetHelp_XRPCClient_Connect(&xhToken, lpszAddr, 50001))
    {
        printf("NetHelp_XRPCClient_Connect\n");
    }

    printf("XEngine_AddMethod(1, 2) = %d\n", XEngine_AddMethod(1, 2));
    XEngine_Print("asdasdasd");
    printf("XEngine_Print\n");

    XEngine_HttpAdd(1, 2);
    printf("wandan\n");
    while (1)
    {
        Sleep(1000);
    }

    return 0;
}
